package com.wangch.dao;

import com.wangch.io.Resources;
import com.wangch.pojo.User;
import com.wangch.sqlsession.SqlSession;
import com.wangch.sqlsession.SqlSessionFactory;
import com.wangch.sqlsession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements IUserDao{
    public List<User> findAll() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setId(1);
        user.setUsername("wangch");
        List<User> users = sqlSession.selectList("user.selectList");
        System.out.println(users);
        return users;
    }

    public User findByCondition(User user) throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user2 = sqlSession.selectOne("user.selectOne", user);
        System.out.println(user2);
        return user2;
    }
}
